1 using UnityEngine;
2 using
System.Collections;
3
4
5 public
class ShakeTweenProperty : AbstractTweenProperty
6 {
7     
private Transform _target;
8     
private Vector3 _shakeMagnitude;
9     
10     
private Vector3 _originalEndValue;
11     
private Vector3 _startPosition;
12     
private Vector3 _startScale;
13     
private Vector3 _startEulers;
14     
15     
private GoShakeType _shakeType;
16     
private int _frameCount;
17     
private int _frameMod;
18     
private bool _useLocalProperties;
19     
public bool useLocalProperties { get { return _useLocalProperties; } }
20     
21     
22     ///
<summary>
23     ///
you can shake any combination of position, scale and eulers by passing in a bitmask of the types you want to shake. frameMod
24     ///
allows you to specify what frame count the shakes should occur on. for example, a frameMod of 3 would mean that only when
25     ///
frameCount % 3 == 0 will the shake occur
26     ///
</summary>
27     
public ShakeTweenProperty( Vector3 shakeMagnitude, GoShakeType shakeType, int frameMod = 1, bool useLocalProperties = false ) : base( true )
28     {
29         _shakeMagnitude = shakeMagnitude;
30         _shakeType = shakeType;
31         _frameMod = frameMod;
32         _useLocalProperties = useLocalProperties;
33     }
34     
35     
36     
#region Object overrides
37     
38     
public override int GetHashCode()
39     {
40         
return base.GetHashCode();
41     }
42     
43     
44     
public override bool Equals( object obj )
45     {
46         
// start with a base check and then compare our material names
47         
if( base.Equals( obj ) )
48             
return this._shakeType == ((ShakeTweenProperty)obj)._shakeType;
49         
50         
return false;
51     }
52     
53     
#endregion
54     
55     
56     
public override bool validateTarget( object target )
57     {
58         
return target is Transform;
59     }
60     
61     
62     
public override void prepareForUse()
63     {
64         _target = _ownerTween.target
as Transform;
65         _frameCount =
0;
66
67         
// store off any properties we will be shaking
68         
if( ( _shakeType & GoShakeType.Position ) != 0 )
69         {
70             
if( _useLocalProperties )
71                 _startPosition = _target.localPosition;
72             
else
73                 _startPosition = _target.position;
74         }
75
76         
if( ( _shakeType & GoShakeType.Eulers ) != 0 )
77         {
78             
if( _useLocalProperties )
79                 _startEulers = _target.eulerAngles;
80             
else
81                 _startEulers = _target.eulerAngles;
82         }
83         
84         
if( ( _shakeType & GoShakeType.Scale ) != 0 )
85             _startScale = _target.localScale;
86     }
87     
88     
89     
private Vector3 randomDiminishingTarget( float falloffValue )
90     {
91         
return new Vector3
92         (
93             Random.Range( -_shakeMagnitude.x, _shakeMagnitude.x ) * falloffValue,
94             Random.Range( -_shakeMagnitude.y, _shakeMagnitude.y ) * falloffValue,
95             Random.Range( -_shakeMagnitude.z, _shakeMagnitude.z ) * falloffValue
96         );
97     }
98     
99
100     
public override void tick( float totalElapsedTime )
101     {
102         
// should we skip any frames?
103         
if( _frameMod > 1 && ++_frameCount % _frameMod != 0 )
104             
return;
105         
106         
// we want 1 minus the eased time so that we go from 1 - 0 for a shake
107         
var easedTime = 1 - _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
108         
109         
110         
// shake any properties required
111         
if( ( _shakeType & GoShakeType.Position ) != 0 )
112         {
113             
var val = _startPosition + randomDiminishingTarget( easedTime );
114             
if( _useLocalProperties )
115                 _target.localPosition = val;
116             
else
117                 _target.position = val;
118         }
119
120         
if( ( _shakeType & GoShakeType.Eulers ) != 0 )
121         {
122             
var val = _startEulers + randomDiminishingTarget( easedTime );
123             
if( _useLocalProperties )
124                 _target.localEulerAngles = val;
125             
else
126                 _target.eulerAngles = val;
127         }
128         
129         
if( ( _shakeType & GoShakeType.Scale ) != 0 )
130         {
131             _target.localScale = _startScale + randomDiminishingTarget( easedTime );
132         }
133     }
134
135 }



Trò chơi Angry Birds trong UNITY Engine 31.702 lượt xem

Gõ tìm kiếm nhanh...